package com.zhixinsou.wuyang.process;

import com.alibaba.fastjson.JSON;
import com.zhixinsou.wuyang.model.AnalysisReport;
import com.zhixinsou.wuyang.model.SheetModel;
import com.zhixinsou.wuyang.util.FileUtil;
import com.zhixinsou.wuyang.util.StringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessJEFFERIES {

    public static List<AnalysisReport> getAnalysisReportPageInfo(String filePath) {
        List<AnalysisReport> reports = new ArrayList<>();
        int startPage = 1;
        File file = new File(filePath);
        String[] lines = FileUtil.pdfReader(file, startPage, startPage).split("\r\n");
        Pattern pattern = Pattern.compile("([0-9]+)\\s-\\s([0-9]+)$");
        while (lines.length > 1 && lines[0].equals("Table of Contents")) {
            for (String line : lines) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    AnalysisReport report = new AnalysisReport();
                    report.setFilePath(filePath);
                    report.setFileName(file.getName());
                    report.setStartPage(Integer.parseInt(matcher.group(1)));
                    report.setEndPage(Integer.parseInt(matcher.group(2)));
                    report.setBrokerName("JEFFERIES");
                    
                    reports.add(report);
                }
            }
            
            startPage++;
            lines = FileUtil.pdfReader(filePath, startPage, startPage).split("\r\n");
        }
        return reports;
    }

    public static void getAnalysisReportDetail(AnalysisReport report) {
        Pattern patternYear = Pattern.compile("JEFFERIES_([0-9]{4})");
        Matcher matcher = patternYear.matcher(report.getFileName());
        if (matcher.find()) {
            int year = Integer.parseInt(matcher.group(1));

            Pattern patternVersion = Pattern.compile("File\\s([0-9]+)[_]{0,1}");
            matcher = patternVersion.matcher(report.getFileName());
            if (matcher.find()) {
                int version = Integer.parseInt(matcher.group(1));

                System.out.println("Processing " + report.getFileName());
                //根据year和version调用不同的解析方法
                if (year <= 2005) {
                    if((year == 2005 && version == 4 && report.getStartPage() >= 375) || (year == 2005 && version > 4))
                        getAnalysisReportDetail2005_2011(report);
                    else
                        getAnalysisReportDetail2003_2005(report);
                } else if (year <= 2011) {
                    if ((year == 2011 && version == 1 && report.getStartPage() >= 405) || (year == 2011 && version > 1)) {
                        getAnalysisReportDetail2012(report);
                    } else
                        getAnalysisReportDetail2005_2011(report);
                } else {
                    getAnalysisReportDetail2012(report);
                }
            }
        }
    }

    public static void getAnalysisReportDetail2012(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //去除空行
        int index = 1;
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}.+?$");
        for (; index < lines.length; index++) {
            String line = lines[index];
            Matcher matcherDate = patternDate.matcher(line.trim());
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                index++;
                break;
            }
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        Pattern patternTicker = Pattern.compile("(.+)\\((.+)\\)(.*)");
        Matcher matcherTicker = patternTicker.matcher(lines[index++].trim());
        if (matcherTicker.find()) {
            report.setCompanyName(matcherTicker.group(1));
            report.setTicker(matcherTicker.group(2));
            report.setRating(matcherTicker.group(3));
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        lines = FileUtil.pdfReader(report.getFilePath(), report.getStartPage() + 1, report.getStartPage() + 1).split("\r\n");
        int last = lines.length - 1;
        Pattern patternName = Pattern.compile("of\\s[0-9]+\\s*([^,]+)");
        boolean isEmpty = true;
        for(; last > -1; last--) {
            boolean find = false;
            Matcher matcher = patternName.matcher(lines[last].trim());
            while (matcher.find()) {
                report.addAnalystName(matcher.group(1).trim());
                find = true;
                isEmpty = false;
            }
            if(!find && !isEmpty)
                break;
        }
        Pattern patternNameCount = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        Matcher matcherNameCount = patternNameCount.matcher(content);
        int authorCount = 0;
        while (matcherNameCount.find()) {
            authorCount++;
        }
        report.addAnalystNameCount(authorCount);
        
        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        lines = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage()).split("\r\n");
        Pattern patternSentence = Pattern.compile("[^\r\n\\.]+?owns shares[^\r\n.]+?\\.");
        for(; index < lines.length; index++) {
            Matcher matcher = patternSentence.matcher(lines[index].trim());
            while (matcher.find()) {
                report.addOwnershipDisclosure(matcher.group());
            }
        }
    }
    
    public static void getAnalysisReportDetail2005_2011(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //去除空行
        int index = 0;
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        report.setDate(lines[index++]);

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        Pattern patternTicker = Pattern.compile("(.+)\\((.+):(.+)\\)");
        Matcher matcherTicker = patternTicker.matcher(lines[index++].trim());
        if (matcherTicker.find()) {
            report.setCompanyName(matcherTicker.group(1));
            report.setExchange(matcherTicker.group(2));
            report.setTicker(matcherTicker.group(3));
        }
        
        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        //获取第一句话
        lines = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getStartPage()).split("\r\n");
        String firstSentence = "";
        boolean summaryStart = false;
        for (int i = index; i < lines.length; i++) {
            String line = lines[i];

            if (line.trim().equals("Investment Summary")) {
                summaryStart = true;
                continue;
            }

            if (summaryStart) {
                firstSentence += line;
                
                if (line.contains("[Helvetica-Bold]")) {
                    break;
                }
            }
        }
        report.setPriorRecommendation(firstSentence);

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        Pattern patternRating = Pattern.compile("Rating:\\s(.+)$");
        for(int i = 0;i < lines.length;i++) {
            Matcher matcherRating = patternRating.matcher(lines[i].trim());
            if (matcherRating.find()) {
                report.setRating(matcherRating.group(1));
                break;
            }
        }
        
        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        lines = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage() + 1, report.getStartPage() + 1).split("\r\n");
        int last = lines.length - 1;
        Pattern patternName = Pattern.compile("^([^,]+)[^@]+@");
        boolean isEmpty = true;
        for(; last > -1; last--) {
            boolean find = false;
            Matcher matcher = patternName.matcher(lines[last].trim());
            while (matcher.find()) {
                report.addAnalystName(matcher.group(1).trim());
                find = true;
                isEmpty = false;
            }
            if(!find && !isEmpty)
                break;
        }
        Pattern patternNameCount = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        Matcher matcherNameCount = patternNameCount.matcher(content);
        int authorCount = 0;
        while (matcherNameCount.find()) {
            authorCount++;
        }
        report.addAnalystNameCount(authorCount);
        
        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        lines = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage()).split("\r\n");
        Pattern patternSentence = Pattern.compile("[^\r\n\\.]+?owns shares[^\r\n.]+?\\.");
        for(; index < lines.length; index++) {
            Matcher matcher = patternSentence.matcher(lines[index].trim());
            while (matcher.find()) {
                report.addOwnershipDisclosure(matcher.group());
            }
        }
    }
    
    public static void getAnalysisReportDetail2003_2005(AnalysisReport report) {
        String[] lines = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage()).split("\r\n");
        
        //去除空行
        int index = 0;
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }
        
//        Pattern patternDate = Pattern.compile("\\s[–-]{1}\\s(.+)$");
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}.+?$");
        Matcher matcherDate = patternDate.matcher(lines[index++].trim());
        if (matcherDate.find()) {
            report.setDate(matcherDate.group());
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }
        
        Pattern patternName = Pattern.compile("([\\sa-zA-Z\\.]+)[^(]*\\([^@]*@[^\\s]+\\s*");
        for(; index < lines.length; index++) {
            boolean find = false;
            Matcher matcher = patternName.matcher(lines[index].trim());
            while (matcher.find()) {
                report.addAnalystName(matcher.group(1).trim());
                find = true;
                report.addAnalystNameCount(1);
            }
            if(!find && report.getAnalystNames() != null)
                break;
        }
        
        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }
        
        report.setCompanyName(lines[index++].trim());

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }
        
        Pattern patternTicker = Pattern.compile("(.*):\\s*(.+)\\s*[-–]{1}");
        Matcher matcherTicker = patternTicker.matcher(lines[index++].trim());
        if (matcherTicker.find()) {
            report.setExchange(matcherTicker.group(1));
            report.setTicker(matcherTicker.group(2));
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }
        
        Pattern patternRating = Pattern.compile("Rating:\\s(.+)$");
        Matcher matcherRating = patternRating.matcher(lines[index++].trim());
        if (matcherRating.find()) {
            report.setRating(matcherRating.group(1));
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        //获取第一句话
        lines = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getEndPage()).split("\r\n");
        String firstSentence = "";
        boolean articleStart = false;
        boolean titleEnd = false;
        Pattern patternLine = Pattern.compile("(.*\\.)\\s");
        for (int i = index; i < lines.length; i++) {
            String line = lines[i];

            if (!titleEnd && line.contains("ArialBlack")) {
                titleEnd = true;
                continue;
            }

            if (titleEnd && !articleStart && line.contains("ArialNarrow")) {
                articleStart = true;
            }

            if (articleStart) {
                line = line.trim().replace("[.*ArialNarrow.*]", "");
                Matcher matcher = patternLine.matcher(line);
                if (matcher.find()) {
                    firstSentence += matcher.group(1);
                    break;
                } else {
                    firstSentence += line;
                }
            }
        }
        report.setPriorRecommendation(firstSentence);
        
        Pattern patternSentence = Pattern.compile("[^\r\n\\.]+?owns shares[^\r\n.]+?\\.");
        for(; index < lines.length; index++) {
            Matcher matcher = patternSentence.matcher(lines[index].trim());
            while (matcher.find()) {
                report.addOwnershipDisclosure(matcher.group());
            }
        }
    }

    public static void process(String rootDir, String resultPath) {
        List<AnalysisReport> reports = new ArrayList<>();
        File[] JEFFERIESFiles = new File(rootDir).listFiles();
        for (File JEFFERIESFile : JEFFERIESFiles) {
            if(JEFFERIESFile.getName().endsWith(".pdf"))
                reports.addAll(getAnalysisReportPageInfo(JEFFERIESFile.getPath()));
        }
        
        //以下代码为写入excel
        List<String> headers = new ArrayList<>();
        headers.add("Date");
        headers.add("Broker name");
        headers.add("Analyst name");
        headers.add("Analyst name count");
        headers.add("Company name");
        headers.add("Ticker");
        headers.add("Exchange");
        headers.add("Rating");
        headers.add("Initiating Coverage");
        headers.add("Prior recommendation");
        headers.add("Ownership disclosure");
        headers.add("PDF file name");
        headers.add("StartPage");
        headers.add("EndPage");
        SheetModel sheetModel = FileUtil.makeSheet(resultPath, "result", headers);

        List<String[]> data = new ArrayList<>();
        for (AnalysisReport report : reports) {
            try {
                ProcessJEFFERIES.getAnalysisReportDetail(report);
            } catch (Exception e) {
                System.out.println(JSON.toJSON(report));
                e.printStackTrace();
                continue;
            }
            String sentence;
            if(report.getOwnershipDisclosures() != null && report.getOwnershipDisclosures().size() == 1)
                sentence = report.getOwnershipDisclosures().get(0);
            else
                sentence = JSON.toJSONString(report.getOwnershipDisclosures());

            String[] line = new String[]{report.getDate(),
                    report.getBrokerName(),
                    JSON.toJSONString(report.getAnalystNames()),
                    report.getAnalystNameCount() + "",
                    report.getCompanyName(),
                    report.getTicker(),
                    report.getExchange(),
                    report.getRating(),
                    report.getInitiatingCoverage(),
                    report.getPriorRecommendation(),
                    sentence,
                    report.getFileName(),
                    report.getStartPage() + "",
                    report.getEndPage() + ""};
            data.add(line);
        }

        FileUtil.exportSheet(sheetModel.getSheet(), data);
        FileUtil.closeSheet(sheetModel);
        System.out.println("Success!");
    }
}
